package com.ruoyi.service.app;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.entity.app.AppUserLoginDB;
import com.ruoyi.common.exception.global.CustomException;
import com.ruoyi.system.dto.AppUpdateUserInfo;
import com.ruoyi.system.mapper.app.AppUserLoginMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class UserService {

    @Resource
    private AppUserLoginMapper appUserLoginMapper;

    @Resource
    private AppCommonService appCommonService;

    public void updateUserInfo(AppUpdateUserInfo dto) {
        // 获取请求头
        boolean exists = appCommonService.checkUserExistsByMiniOpenId(null);
        if (!exists){
            throw new CustomException("用户不存在，请先注册");
        }
        // 更改数据
        LambdaUpdateWrapper<AppUserLoginDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ObjectUtils.isNotEmpty(dto.getAvatarUrl()), AppUserLoginDB::getAvatarUrl, dto.getAvatarUrl());
        updateWrapper.set(ObjectUtils.isNotEmpty(dto.getNickName()), AppUserLoginDB::getUserNick, dto.getNickName());
        appUserLoginMapper.update(null, updateWrapper);
    }

    public AppUserLoginDB getUserInfo() {
        return appCommonService.getUserByMiniOpenId(null);
    }
}
